﻿using CaseManagerLibrary.Encrypt;
using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.VersionManager
{
    public partial class FrmVersion : Form
    {
        public dl_psd_version Version { get; set; }
        public List<dl_psd_version_file_vo> VersionFiles { get; set; }

        public FrmVersion()
        {
            InitializeComponent();

            VersionFiles = new List<dl_psd_version_file_vo>();

            TreeNode root = new TreeNode();
            root.Text = "\\";
            root.Name = "\\";
            root.Tag = "root";
            tvwFile.Nodes.Add(root);
        }

        public void SetVersion(dl_psd_version_vo version)
        {
            Version = version;
            txtVersion.Text = version.psd_version;
            txtDescription.Text = version.description;
            dtpCreateTime.Value = version.create_time;
            txtAction.Text = version.action;

            GetVersionFile();
        }

        private void GetVersionFile()
        {
            var result_file = HttpHelper.Get<HResult<List<dl_psd_version_file>>>(AppSettings.Setting.API.GetPSDVersionFiles + "?version=" + Version.psd_version);

            if (result_file.success)
            {
                foreach (var file in result_file.data)
                {
                    string[] path = file.file_path.Split(tvwFile.PathSeparator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    /*
                     *  \folder1\folder2\filename.dll 
                    */
                    CreateTree(path, 0, file.file_name, tvwFile.Nodes[0].Nodes);
                }
            }
        }

        private void CreateTree(string[] path, int level, string fileName, TreeNodeCollection nodes)
        {
            bool exist = false;

            foreach (TreeNode node in nodes)
            {
                if (node.Name == path[level])
                {
                    CreateTree(path, level + 1, fileName, node.Nodes);
                    exist = true;
                }
                else
                {
                    exist = false;
                }
            }

            if (!exist)
            {
                TreeNode node_folder = new TreeNode();
                node_folder.Text = path[level];
                node_folder.Name = path[level];
                node_folder.Tag = "folder";
                nodes.Add(node_folder);

                if (level == path.Length - 1)
                {
                    TreeNode node_file = new TreeNode();
                    node_file.Text = fileName;
                    node_file.Name = fileName;
                    node_file.Tag = "file";
                    node_folder.Nodes.Add(node_file);
                }
            }
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            if (Version == null)
            {
                Version = new dl_psd_version();
                Version.id = Guid.NewGuid().ToString();
            }

            Version.psd_version = txtVersion.Text.Trim();
            Version.description = txtDescription.Text;
            Version.create_time = dtpCreateTime.Value;
            Version.action = txtAction.Text;
            Version.update_success = 0;
            Version.update_fail = 0;

            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnAddFile_Click(object sender, EventArgs e)
        {
            if (tvwFile.SelectedNode != null)
            {
                var ofg = new OpenFileDialog();
                ofg.Multiselect = true;

                if (ofg.ShowDialog() == DialogResult.OK)
                {
                    foreach (string file in ofg.FileNames)
                    {
                        TreeNode node_file = new TreeNode();
                        tvwFile.SelectedNode.Nodes.Add(node_file);
                        node_file.Text = System.IO.Path.GetFileName(file);
                        node_file.Name = node_file.Text;
                        node_file.Tag = "file";

                        dl_psd_version_file_vo version_file = new dl_psd_version_file_vo();
                        version_file.id = Guid.NewGuid().ToString();
                        version_file.psd_version = txtVersion.Text;
                        version_file.file_name = node_file.Text;
                        version_file.file_path = node_file.Parent.FullPath;
                        version_file.file_size = new FileInfo(file).Length;
                        version_file.sign = FileValidHelper.GetFileSHA1(file);
                        version_file.local_path = file;

                    }
                }
            }
        }

        private void btnAddFolder_Click(object sender, EventArgs e)
        {
            if (tvwFile.SelectedNode != null)
            {
                txtFolder.Clear();
                gbAddFolder.Visible = true;
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (tvwFile.SelectedNode != null)
            {
                if (tvwFile.SelectedNode.Tag.ToString() == "file")
                {
                    var file = VersionFiles.Where(t => (t.file_path + "\\" + t.file_name) == tvwFile.SelectedNode.FullPath).FirstOrDefault();
                    if (file != null)
                    {
                        VersionFiles.Remove(file);
                    }
                    tvwFile.SelectedNode.Remove();
                }
                else if (tvwFile.SelectedNode.Tag.ToString() == "folder")
                {
                    foreach (TreeNode node_file in tvwFile.SelectedNode.Nodes)
                    {
                        var file = VersionFiles.Where(t => (t.file_path + "\\" + t.file_name) == node_file.FullPath).FirstOrDefault();
                        if (file != null)
                        {
                            VersionFiles.Remove(file);
                        }
                        node_file.Remove();
                    }
                    tvwFile.SelectedNode.Remove();
                }
                else if (tvwFile.SelectedNode.Tag.ToString() == "root")
                {
                    VersionFiles.Clear();
                    tvwFile.SelectedNode.Nodes.Clear();
                }
            }
        }

        private void btnAddFolderAccept_Click(object sender, EventArgs e)
        {
            if (txtFolder.Text.Length > 0)
            {
                TreeNode node_folder = new TreeNode();
                node_folder.Text = txtFolder.Text.Trim();
                node_folder.Name = node_folder.Text;
                node_folder.Tag = "folder";
                tvwFile.SelectedNode.Nodes.Add(node_folder);
                gbAddFolder.Visible = false;
            }
        }

        private void btnAddFolderCancel_Click(object sender, EventArgs e)
        {
            gbAddFolder.Visible = false;
        }

        private void tvwFile_DoubleClick(object sender, EventArgs e)
        {
            if (tvwFile.SelectedNode != null && tvwFile.SelectedNode.Tag.ToString() == "folder")
            {
                gbAddFolder.Visible = true;
                txtVersion.Text = tvwFile.SelectedNode.Text;
            }
        }
    }
}
